package com.zmc.admin.controller.system;

import com.zmc.admin.infrastructure.bean.vo.dept.SysDeptVo;
import com.zmc.admin.infrastructure.constant.ErrorCodeConstants;
import com.zmc.admin.infrastructure.constant.UserConstants;
import com.zmc.admin.infrastructure.entity.SysDeptEntity;
import com.zmc.admin.infrastructure.bean.query.SysDeptQuery;
import com.zmc.admin.infrastructure.service.SysDeptService;
import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.exception.base.WebDisplayException;
import com.zmc.common.utils.R;
import com.zmc.common.utils.StringUtils;
import com.zmc.common.utils.token.AuthTokenService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息
 * @author zmc
 */
@RestController
@RequestMapping("/dept")
public class SysDeptController {
    @Autowired
    private SysDeptService deptService;

    @Autowired
    private AuthTokenService authTokenService;

    /**
     * 获取部门列表
     */
    @PostMapping("/list")
    public R list(@RequestBody SysDeptQuery dept) {
        List<SysDeptEntity> list = deptService.selectDeptList(dept);
        return R.ok(list);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @PostMapping("/list/exclude")
    public R excludeChild(@RequestBody SysDeptVo vo) {
        Long deptId = vo.getDeptId();
        List<SysDeptEntity> depts = deptService.list();
        depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return R.ok(depts);
    }

    /**
     * 根据部门编号获取详细信息
     */
    @PostMapping(value = "/info")
    public R getInfo(@RequestBody SysDeptVo vo) {
        return R.ok(deptService.getById(vo.getDeptId()));
    }

    /**
     * 新增部门
     */
    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R add(@RequestBody SysDeptEntity dept) {
        if (!deptService.checkDeptNameUnique(dept)) {
            throw new WebDisplayException(ErrorCodeConstants.DEPT_EXIT_EXCEPTION);
        }
        dept.setCreateBy(authTokenService.getUserInfo().getUsername());
        deptService.save(dept);
        return R.ok();
    }

    /**
     * 修改部门
     */
    @PostMapping("/update")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R edit(@RequestBody SysDeptEntity dept) {
        Long deptId = dept.getDeptId();
        if (!deptService.checkDeptNameUnique(dept)) {
            throw new WebDisplayException(ErrorCodeConstants.DEPT_EXIT_EXCEPTION);
        } else if (dept.getParentId().equals(deptId)) {
            throw new WebDisplayException(ErrorCodeConstants.DEPT_PARENT_IS_OWNER_EXCEPTION);
        } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0) {
            throw new WebDisplayException(ErrorCodeConstants.DEPT_HAS_STOP_CHILD_EXCEPTION);
        }
        dept.setUpdateBy(authTokenService.getUserInfo().getUsername());
        deptService.updateById(dept);
        return R.ok();
    }

    /**
     * 删除部门
     */
    @PostMapping("/delete")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R remove(@RequestBody SysDeptVo vo) {
        Long deptId = vo.getDeptId();
        if (deptService.hasChildByDeptId(deptId)) {
            throw new WebDisplayException(ErrorCodeConstants.DEPT_HAS_CHILD_EXCEPTION);
        }
        if (deptService.checkDeptExistUser(deptId)) {
            throw new WebDisplayException(ErrorCodeConstants.DEPT_HAS_USER_EXCEPTION);
        }
        deptService.removeById(deptId);
        return R.ok();
    }
}
